package com.zcgame.base.net

import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.JsonParser
import java.lang.RuntimeException

/**
 *@author:huangLJ
 *@date:2021/4/27 13:58
 */
object GsonUtils {
    //bean-->String
    fun toJsonString(obj:Any): String = Gson().toJson(obj)

    fun <T> parserJsonToBean(jsonString:String,classBean:Class<T>):T{
        if (TextUtils.isEmpty(jsonString)) {
            throw RuntimeException("jsonString empty")
        }
        val jsonElement = JsonParser.parseString(jsonString)
        if (jsonElement.isJsonNull || !jsonElement.isJsonObject) {
            throw RuntimeException("JsonToArrayBean is empty or object")
        }
        return Gson().fromJson(jsonElement,classBean)
    }
}